package mapper;

import org.apache.ibatis.annotations.*;
import pojo.Seat;

import java.util.List;

public interface SeatMapper {
    @Insert("INSERT INTO seat(floor_name,number) " +
            "VALUES (#{floor_name},#{number})")
    boolean insert(@Param("floor_name") String floorName, @Param("number") int number) throws Exception;

    @Select("SELECT * FROM seat WHERE floor_name=#{floor_name} AND number=#{number}")
    Seat query(@Param("floor_name") String floor_name,
               @Param("number") int number);

    @Select("SELECT * FROM seat")
    List<Seat> queryAll();

    @Select("SELECT * FROM seat WHERE floor_name=#{floor_name}")
    List<Seat> querySeatsByFloor(@Param("floor_name") String floorName);

    @Update("UPDATE seat SET reservation_flag=#{reservation_flag}," +
            "open_flag=#{open_flag} " +
            "WHERE floor_name=#{floor_name} AND open_flag=#{open_flag}")
    boolean update(Seat seat);

    @Update("UPDATE seat SET open_flag='1' " +
            "WHERE floor_name=#{floor_name} AND number=#{number}")
    boolean openSeat(@Param("floor_name") String floorName,
                      @Param("number") int number);

    @Update("UPDATE seat SET open_flag='0' " +
            "WHERE floor_name=#{floor_name} AND number=#{number}")
    boolean closeSeat(@Param("floor_name") String floorName,
                      @Param("number") int number);

    @Select("SELECT max(number) FROM seat WHERE floor_name=#{floor_name}")
    Integer findMax(@Param("floor_name") String floorName);

    @Select("SELECT COUNT(*) FROM seat WHERE floor_name=#{floor_name} GROUP BY floor_name")
    Integer getTotalNumber(@Param("floor_name") String floorName);

    @Select("SELECT COUNT(*) FROM seat WHERE floor_name=#{floor_name} AND reservation_flag='1' GROUP BY floor_name ")
    Integer getOccupiedNumber(@Param("floor_name") String floorName);

    @Select("SELECT COUNT(*) FROM seat WHERE floor_name=#{floor_name} AND open_flag='0' GROUP BY floor_name ")
    Integer getClosedNumber(@Param("floor_name") String floorName);
}
